package com.hm.jiaowuxitong.ui;

import java.awt.*;
import javax.swing.*;

import com.hm.jiaowuxitong.service.*;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GifTest {
    public static void main(String[] args) {new GifTest();}
    public GifTest(){
        //创建面板
        JFrame frame = new JFrame("教务系统");
        frame.setSize(1200, 700);
        //创建画布
        JPanel panel = new JPanel(new BorderLayout());
        //创建方向布局
        SpringLayout layout = new SpringLayout();
        panel.setLayout(layout);
        //创建文本框控件
        JLabel label1 = new JLabel("账户:");
        label1.setFont(new Font(" ", 3, 30));
        label1.setForeground(Color.black);
        layout.putConstraint(SpringLayout.WEST, label1, 400, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label1, 200, SpringLayout.NORTH, panel);
        //创建文本编辑框控件
        JTextField jtf1 = new JTextField(16);
        layout.putConstraint(SpringLayout.NORTH, jtf1, 208, SpringLayout.NORTH, panel);
        layout.putConstraint(SpringLayout.WEST, jtf1, 5, SpringLayout.EAST, label1);
        jtf1.setFont(new Font(" ", 2, 20));
        //创建文本框控件
        JLabel label2 = new JLabel("密码:");
        layout.putConstraint(SpringLayout.WEST, label2, 400, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label2, 20, SpringLayout.SOUTH, label1);
        label2.setForeground(Color.black);
        label2.setFont(new Font(" ", 3, 30));
        //创建文本框控件
        JLabel label3 = new JLabel("身份认证:");
        layout.putConstraint(SpringLayout.WEST, label3, 340, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label3, 20, SpringLayout.SOUTH, label2);
        label3.setForeground(Color.black);
        label3.setFont(new Font(" ", 3, 30));
        JLabel label4 = new JLabel("欢迎来到教务系统");
        layout.putConstraint(SpringLayout.WEST, label4, 440, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, label4, 50, SpringLayout.NORTH, panel);
        label4.setForeground(Color.black);
        label4.setFont(new Font(" ", 3, 40));
        //创建登录按钮
        //AirplaneButton button1 = new AirplaneButton();
        JButton button1 = new JButton("登录");
        layout.putConstraint(SpringLayout.WEST, button1, 510, SpringLayout.WEST, panel);
        layout.putConstraint(SpringLayout.NORTH, button1, 490, SpringLayout.NORTH, panel);
        button1.setFont(new Font(" ", 3, 25));
        JButton button2 = new JButton("注册");
        layout.putConstraint(SpringLayout.WEST, button2, 20, SpringLayout.EAST, button1);
        layout.putConstraint(SpringLayout.NORTH, button2, 490, SpringLayout.NORTH, panel);
        button2.setFont(new Font(" ", 3, 25));
        //密码显示
        JPasswordField passwordField = new JPasswordField(16);
        JButton toggleButton = new JButton("👁");
        layout.putConstraint(SpringLayout.WEST, toggleButton, 242, SpringLayout.EAST, label2);
        layout.putConstraint(SpringLayout.NORTH, toggleButton, 265, SpringLayout.NORTH, panel);
        toggleButton.setFont(new Font(" ", 3, 13));

        layout.putConstraint(SpringLayout.WEST, passwordField, 5, SpringLayout.EAST, label2);
        layout.putConstraint(SpringLayout.NORTH, passwordField, 265, SpringLayout.NORTH, panel);
        passwordField.setFont(new Font(" ", 3, 20));
        // 创建下拉框并添加选项
        String[] options = {"教师", "学生", "管理员"};
        JComboBox<String> comboBox = new JComboBox<>(options);

        // 设置下拉框的位置和大小
        layout.putConstraint(SpringLayout.WEST, comboBox, 5, SpringLayout.EAST, label3);
        layout.putConstraint(SpringLayout.NORTH, comboBox, 26, SpringLayout.SOUTH, passwordField);
        comboBox.setForeground(Color.black);
        comboBox.setPreferredSize(new Dimension(277, 32));

        toggleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (passwordField.getEchoChar() == 0) {
                    passwordField.setEchoChar('*');
                } else {
                    passwordField.setEchoChar((char) 0);
                }
            }
        });
//        // 创建一个自定义图标
//        ImageIcon icon = new ImageIcon(".idea/img_2.png");
//        Image image = icon.getImage(); // 获取图像对象
//        Image scaledImage = image.getScaledInstance(70, 30, Image.SCALE_SMOOTH); // 调整图像大小
//        icon.setImage(scaledImage); // 设置调整后的图像为按钮的图标

        toggleButton.setBorderPainted(false);//不显示按钮的边框。
        toggleButton.setRolloverEnabled(true);//启用按钮悬停效果。
        toggleButton.setForeground(Color.black);
        toggleButton.setContentAreaFilled(false);
        toggleButton.setFocusPainted(false);
        toggleButton.setOpaque(false);
        toggleButton.setFocusPainted(false);

//        button1.setIcon(icon);
//        button2.setIcon(icon);
        //把所有控件添加至画布
        panel.add(label1);
        panel.add(jtf1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
        panel.add(toggleButton);
        panel.add(passwordField);
        panel.add(button1);
        panel.add(button2);

        // 将下拉框添加到 JFrame 中
        panel.add(comboBox);


        // 创建 JFXPanel 对象，用于加载视频
        JFXPanel jfxPanel = new JFXPanel();

        // 将 JFXPanel 添加到 JPanel 中心区域
        panel.add(jfxPanel, BorderLayout.CENTER);

        // 创建媒体对象并加载视频文件
        String videoFile = "image/2.mp4";
        Media media = new Media(new File(videoFile).toURI().toString());

        // 创建媒体播放器对象
        MediaPlayer mediaPlayer = new MediaPlayer(media);

        // 创建媒体视图对象并设置大小
        MediaView mediaView = new MediaView(mediaPlayer);
        mediaView.setFitWidth(1200);
        mediaView.setFitHeight(898);
        // 将媒体视图添加到 JFXPanel 中
        jfxPanel.setScene(new Scene(new BorderPane(mediaView)));
        // 监听视频播放完成事件
        mediaPlayer.setOnEndOfMedia(() -> {
            // 循环播放
            mediaPlayer.seek(mediaPlayer.getStartTime());
            mediaPlayer.play();
        });
        // 播放视频
        mediaPlayer.play();
        // 将视频组件添加到面板的中心部分
        panel.add(jfxPanel, BorderLayout.CENTER);
        //把画布添加至面板
        frame.setContentPane(panel);
        //禁止面板放大缩小
        frame.setResizable(false);
        //允许关闭整个应用
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //将面板显示
        frame.setVisible(true);
        // 设置窗口居中显示
        frame.setLocationRelativeTo(null);
        // 显示窗口
        frame.setVisible(true);
        // 确保JavaFX线程正常关闭
        Platform.setImplicitExit(false);
        //添加点击事件
        button1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                //当鼠标按下当前按钮，弹出添加教师信息的窗口
                String id = jtf1.getText();
                String key = passwordField.getText();
                //多个数据封装成一个对象，便于传输
                Map<String, Object> oMap = new HashMap<>();
                oMap.put("id", id);
                oMap.put("key", key);
                if (((JButton) (e.getSource())).getText().equals("登录")) {
                    button1.setBorder(BorderFactory.createLoweredSoftBevelBorder());
                    button1.setForeground(Color.white);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (((JButton) (e.getSource())).getText().equals("登录")) {
                    button1.setForeground(Color.black);
                    button1.setBackground(Color.white);
                    button1.setContentAreaFilled(false);
                    button1.setFocusPainted(false);
                    button1.setOpaque(false);
                    button1.setFocusPainted(false);;}
                if (jtf1.getText().length() == 0 || passwordField.getText().length() == 0) {
                    JOptionPane.showMessageDialog(null, "账号或密码为空", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                } else {
                    String selectedOption = (String) comboBox.getSelectedItem();
                    if (selectedOption.equals("教师")) {
                        ITeacherService service = new TeacherServiceImp();
                        List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                        if (map.size()==0) {
                            JOptionPane.showMessageDialog(null, "账户不存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        } else {
                            String password = passwordField.getText();
                            if (map.get(0).get("key").equals(password)) {
                                JOptionPane.showMessageDialog(null, "登录成功", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                                new TeacherGui();
                                //释放内存
                                frame.dispose();}
                            else {
                                JOptionPane.showMessageDialog(null, "密码错误", "消息提示", JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    }  if (selectedOption.equals("学生")) {
                        IStudentService service = new StudentServiceImp();
                        List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                        if (map.size()==0) {
                            JOptionPane.showMessageDialog(null, "账户不存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        } else {
                            String password = passwordField.getText();
                            if (map.get(0).get("key").equals(password)) {
                                JOptionPane.showMessageDialog(null, "登录成功", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                                new StudentGui();
                                //释放内存
                                frame.dispose();}
                            else {
                                JOptionPane.showMessageDialog(null, "密码错误", "消息提示", JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    } else if (selectedOption.equals("管理员")) {
                        IAdminService service = new AdminServiceImp();
                        List<Map<String, Object>> map = service.HgetUser(jtf1.getText());
                        if (map.size()==0) {
                            JOptionPane.showMessageDialog(null, "账户不存在", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                        } else {
                            String password = passwordField.getText();
                            if (map.get(0).get("key").equals(password)) {
                                JOptionPane.showMessageDialog(null, "登录成功", "消息提示", JOptionPane.WARNING_MESSAGE);    //消息对话框
                                new AdministratorGui();
                                //释放内存
                                frame.dispose();}
                            else {
                                JOptionPane.showMessageDialog(null, "密码错误", "消息提示", JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    }
                }
            }


            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        button2.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (((JButton) (e.getSource())).getText().equals("注册")) {
                    button2.setBorder(BorderFactory.createLoweredSoftBevelBorder());
                    button2.setForeground(Color.white);
                }
                new newUser();
                frame.dispose();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (((JButton) (e.getSource())).getText().equals("注册")) {
                    button2.setForeground(Color.black);
                    button2.setBackground(Color.white);
                    button2.setContentAreaFilled(false);
                    button2.setFocusPainted(false);
                    button2.setOpaque(false);
                    button2.setFocusPainted(false);}
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {


            }
        });

    }
}